package indi.zhifa.recipe.bailan.framework.auth.impl.dao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import indi.zhifa.recipe.bailan.framework.auth.impl.dao.mapper.RoleMapper;
import indi.zhifa.recipe.bailan.framework.auth.impl.dao.service.IRoleDbService;
import indi.zhifa.recipe.bailan.framework.auth.impl.entity.po.RoleEntity;
import indi.zhifa.recipe.bailan.framework.mysql.entity.annotations.ZfDbService;
import indi.zhifa.recipe.bailan.framework.mysql.service.impl.ZfDbServiceImpl;
import indi.zhifa.recipe.bailan.framework.redis.util.RedisUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;

import java.io.Serializable;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author 织法
 * @since 2022-12-19
 */
@RequiredArgsConstructor
@CacheConfig(cacheNames = "sys_role")
@ZfDbService(name = "RoleEntity")
public class RoleDbServiceImpl extends ZfDbServiceImpl<RoleMapper, RoleEntity> implements IRoleDbService {

    private final RedisUtil mRedisUtil;

    @Cacheable(value = "sys_role-byCode",key = "#pCode",condition = "#result!=null")
    @Override
    public Long findByCode(String pCode) {
        LambdaQueryWrapper<RoleEntity> queryWrapper = Wrappers.<RoleEntity>lambdaQuery()
                .eq(RoleEntity::getCode,pCode);
        RoleEntity roleEntity = findOne(queryWrapper);
        if(null != roleEntity){
            return roleEntity.getId();
        }
        return null;
    }

    @Cacheable(key = "#pId",condition = "#result!=null")
    @Override
    public RoleEntity check(Serializable pId) {
        return super.check(pId);
    }

    @CachePut(key = "#pId")
    @Override
    public RoleEntity savePull(Long pId,RoleEntity pEntity) {
        return super.savePull(pId,pEntity);
    }

    @CachePut(key = "#pId")
    @Override
    public RoleEntity updatePull(Long pId, RoleEntity pEntity) {
        return super.updatePull(pId,pEntity);
    }

    @CacheEvict(key = "#pId")
    @Override
    public RoleEntity deleteById(Serializable pId) {
        return super.deleteById(pId);
    }

    @Override
    protected RoleEntity onDelete(RoleEntity pDeletingEntity){
        mRedisUtil.delete("sys_role-byCode::"+pDeletingEntity.getCode());
        return pDeletingEntity;
    }

}
